<template>
  <div class="app-container">
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="地址" prop="area">
        <area-cascader v-model="queryParams.area" />
      </el-form-item>
      <el-form-item label="" v-if="queryParams.area">
        <el-upload
          :auto-upload="false"
          ref="fileUpload"
          multiple
          :action="uploadFileUrl"
          :file-list="fileList"
          :limit="1"
          accept="application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
          :on-error="handleUploadError"
          :on-exceed="handleExceed"
          :on-change="handleFileChange"
          :on-success="handleUploadSuccess"
          :show-file-list="false"
          :headers="headers"
          class="upload-file-uploader"
        >
          <!-- 上传按钮 -->
          <el-button size="mini" type="primary">选取文件</el-button>
        </el-upload>
      </el-form-item>
      <el-form-item label="" prop="" v-if="queryParams.area">
        <el-input v-model="queryParams.fileName" readonly style="width: 300px;"/>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-upload"
          size="mini"
          @click="doUpload" v-if="queryParams.area">导入</el-button>
        <el-button icon="el-icon-download" size="mini" @click="downloadTpl">下载模版</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="sjskList"
    >
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="gender">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gender" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column
        label="身份证号码"
        align="center"
        prop="idno"
      />
      <el-table-column
        label="户籍地派出所"
        align="center"
        prop="hjdpcs"
      />
      <el-table-column
        label="人员类型"
        align="center"
        prop="rylx"
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
      >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.existUserId" type="success">已存在</el-tag>
          <el-tag v-if="!scope.row.existUserId" type="danger">不存在</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.existUserId"
            size="mini"
            type="text"
            icon="el-icon-edit"
          >导入</el-button
          >
          <el-button
            v-if="scope.row.existUserId"
            size="mini"
            type="text"
            icon="el-icon-edit"
          >完善</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {getToken} from "@/utils/auth";

export default {
  name: "SjskCompareIndex",
  dicts: ['gender'],
  data() {
    return {
      uploadFileUrl: process.env.VUE_APP_BASE_API + "/user/compare/uploadToCompare", // 上传文件服务器地址
      fileType: ["xlsx"],
      fileList: [],
      fileName: '',
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 社戒社康人员表格数据
      sjskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      // 查询参数
      queryParams: {
        area: undefined,
        fileName: undefined
      },
      // 表单参数
      form: {},
    };
  },
  methods: {
    doUpload() {
      this.$refs.fileUpload.submit();
    },
    uploadedSuccessfully(data) {
      this.resetForm("queryForm");
      this.queryParams = {
        area: undefined,
        fileName: undefined
      }
      this.sjskList = data
    },
    handleUploadSuccess(res, file) {
      if (res.code === 200) {
        this.uploadedSuccessfully(res.data);
      } else {
        this.$modal.closeLoading();
        this.$modal.msgError(res.msg);
        this.$refs.fileUpload.handleRemove(file);
        this.uploadedSuccessfully();
      }
    },
    handleFileChange(file) {
      this.queryParams.fileName = file.name
    },
    handleExceed() {
      this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
    },
    handleUploadError(err) {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    downloadTpl() {
      this.download(
        "user/compare/exportCompareTpl",
        {
          ...this.queryParams,
        },
        `社戒社康人员数据对比模版.xlsx`
      )
    },
    handleQuery() {},
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        area: undefined,
        fileName: undefined
      }
      this.sjskList = []
      this.handleQuery();
    },
  },
};
</script>
